package com.yiwei.jmx.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.GridViewAddImgesAdpter;
import com.yiwei.jmx.adapter.SuggestAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.SuggestBean;
import com.yiwei.jmx.api.bean.UploadImageBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.ImageUtil;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.NestedGridView;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;
import com.yiwei.jmx.view.uploadImage.UploadImgAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SuggestActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(R.id.upload_gridview)
    NestedGridView uploadGridview;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;

    private Unbinder unbiner;
    private UploadImgAdapter uploadImgAdapter;
    private GridViewAddImgesAdpter gridViewAddImgesAdpter;
    private List<String> datas;
    private SuggestAdapter adapter;
    private SuggestBean submitBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggest);
        initWebViewBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("意见反馈");
        submitBean = new SuggestBean();
        adapter = new SuggestAdapter(this);
        adapter.setOnChooseListener(new SuggestAdapter.OnChooseListener() {
            @Override
            public void onSuccess(String type) {
                submitBean.type = type;
            }
        });
        list.setAdapter(adapter);
        List<String> suggestType = new ArrayList<>();
        suggestType.add("现有功能优化");
        suggestType.add("新功能建议");
        suggestType.add("就是随便聊聊");
        adapter.update(suggestType);

        datas = new ArrayList<>();
        gridViewAddImgesAdpter = new GridViewAddImgesAdpter(datas, this);
        uploadGridview.setAdapter(gridViewAddImgesAdpter);
        uploadGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (position == gridViewAddImgesAdpter.getCount() - 1 && position < 4 && !gridViewAddImgesAdpter.isLastUpload()) {
//                    ((SendEvaluateActivity) getActivity()).goToTakePhoto(fragmentPoi);
                    goToTakePhoto();
                }
            }
        });
    }


    public void savePath(String path) {
        gridViewAddImgesAdpter.getDatas().add(path);
        gridViewAddImgesAdpter.notifyDataSetChanged();
//        ((SendEvaluateActivity) getActivity()).getSubmitData().get(fragmentPoi).photos = gridViewAddImgesAdpter.getDatas();
    }

    public void goToTakePhoto() {

        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }

            @Override
            public void onReject() {

            }

        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {

                }
            }
        }, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void getUploadImg(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        UploadImageBean bean = new UploadImageBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    savePath(result.getUrl());
                });
    }

    public void getSuggest() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        Api.api_service.getSuggest(submitBean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    new SelectMenuTipsDilaog(this).setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
                        @Override
                        public void onLeft() {
                            finish();
                        }

                        @Override
                        public void onRight() {
                            finish();
                        }
                    }).showOneSelectData("主人", R.mipmap.dialog_tips_success, "意见反馈提交成功，我们会认真讨论对待稍后可能有工作人员与您联系", "好的，我知道啦").show();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        submitBean.content = UI.toString(editContent);
        submitBean.phone = UI.toString(editPhone);
        submitBean.photos = gridViewAddImgesAdpter.getDatas();
        if (submitBean.checkAll()) {
            getSuggest();
        }


    }
}
